package com.newviews.tcmip.web.controller;

import com.newviews.tcmip.common.constant.CommonConsts;
import com.newviews.tcmip.common.model.PageModel;
import com.newviews.tcmip.common.model.ResultModel;

import com.newviews.tcmip.common.utils.UUIDGenerator;
import com.newviews.tcmip.pojo.po.EtcmHerbPo;
import com.newviews.tcmip.pojo.po.EtcmUiElPo;
import com.newviews.tcmip.service.inf.EtcmDiseaseExServiceInf;
import com.newviews.tcmip.service.inf.EtcmDiseaseServiceInf;
import com.newviews.tcmip.service.inf.EtcmPxServiceInf;
import com.newviews.tcmip.web.vo.combin.HerbHerbPageReqVo;
import com.newviews.tcmip.web.vo.combin.HerbHerbReqVo;
import com.newviews.tcmip.web.vo.combin.NetworkStatisticsRespVo;
import com.newviews.tcmip.web.vo.combin.SearchRespVo;
import com.newviews.tcmip.web.vo.combin.download.HerbHerbDownLoadReqVo;
import com.newviews.tcmip.web.vo.etcmpx.PxNetworkStatisticsRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.Get;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/etcm/Combin")
@Api(tags = "组合")
@Slf4j
public class EtcmCombinationController {
    @Resource
    private EtcmPxServiceInf etcmPxServiceInf;

    @Autowired
    private EtcmDiseaseExServiceInf etcmDiseaseExServiceInf;



    @PostMapping("/herb/herb")
    @ApiOperation("herb_herb")
    public ResultModel<NetworkStatisticsRespVo> getHerbHerb(@RequestBody HerbHerbReqVo reqVo){
        return etcmPxServiceInf.getHerbHerb(reqVo);
    }

    @PostMapping("/herb/herb/page")
    @ApiOperation("herb_herb_table")
    public ResultModel<PageModel<Map<String,String>>> getHerbHerbPage(@RequestBody HerbHerbPageReqVo reqVo){
        return etcmPxServiceInf.getHerbHerbPage(reqVo);
    }

    @PostMapping("/herb/herb/download")
    @ApiOperation("herb_herb_download")
    public void getHerbHerbDownLoad(@ApiIgnore HttpServletResponse response,@RequestBody HerbHerbDownLoadReqVo reqVo){

        try {
            HerbHerbPageReqVo pageReqVo = new HerbHerbPageReqVo();
            BeanUtils.copyProperties(reqVo,pageReqVo);
            pageReqVo.setPageNo(1);
            pageReqVo.setPageSize(1000);
            ResultModel<PageModel<Map<String, String>>> res = etcmPxServiceInf.getHerbHerbPage(pageReqVo);
            if (!res.isSuccess()){
                log.error(res.getMessage());
                return;
            }

            etcmDiseaseExServiceInf.getDownload(response,reqVo.getUiTypeCode(),reqVo.getLangCode(),reqVo.getExportType(),res.getData().getRecords());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @PostMapping("/ingr/ingr")
    @ApiOperation("ingr_ingr")
    public ResultModel<NetworkStatisticsRespVo> getIngrIngr(@RequestBody HerbHerbReqVo reqVo){
        return etcmDiseaseExServiceInf.getIngrIngr(reqVo);
    }

    @PostMapping("/ingr/ingr/page")
    @ApiOperation("ingr_ingr_table")
    public ResultModel<PageModel<Map<String,String>>> getIngrIngrPage(@RequestBody HerbHerbPageReqVo reqVo){
        return etcmDiseaseExServiceInf.getIngrIngrPage(reqVo);
    }

    @PostMapping("/ingr/ingr/download")
    @ApiOperation("ingr_ingr_download")
    public void getIngrIngrDownLoad(@ApiIgnore HttpServletResponse response,@RequestBody HerbHerbDownLoadReqVo reqVo){
        try {
            HerbHerbPageReqVo pageReqVo = new HerbHerbPageReqVo();
            BeanUtils.copyProperties(reqVo,pageReqVo);
            pageReqVo.setPageNo(1);
            pageReqVo.setPageSize(1000);
            ResultModel<PageModel<Map<String, String>>> res = etcmDiseaseExServiceInf.getIngrIngrPage(pageReqVo);
            if (!res.isSuccess()){
                log.error(res.getMessage());
                return;
            }

            etcmDiseaseExServiceInf.getDownload(response,reqVo.getUiTypeCode(),reqVo.getLangCode(),reqVo.getExportType(),res.getData().getRecords());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @PostMapping("/px/disease")
    @ApiOperation("px_disease")
    public ResultModel<NetworkStatisticsRespVo> getPxDisease(@RequestBody HerbHerbReqVo reqVo){
        return etcmDiseaseExServiceInf.getPxDisease(reqVo);
    }

    @PostMapping("/px/disease/page")
    @ApiOperation("px_disease_table")
    public ResultModel<PageModel<Map<String,String>>> getPxDiseasePage(@RequestBody HerbHerbPageReqVo reqVo){
        return etcmDiseaseExServiceInf.getPxDiseasePage(reqVo);
    }

    @PostMapping("/px/disease/download")
    @ApiOperation("px_disease_download")
    public void getPxDiseaseDownLoad(@ApiIgnore HttpServletResponse response,@RequestBody HerbHerbDownLoadReqVo reqVo){
        try {
            HerbHerbPageReqVo pageReqVo = new HerbHerbPageReqVo();
            BeanUtils.copyProperties(reqVo,pageReqVo);
            pageReqVo.setPageNo(1);
            pageReqVo.setPageSize(1000);
            ResultModel<PageModel<Map<String, String>>> res = etcmDiseaseExServiceInf.getPxDiseasePage(pageReqVo);
            if (!res.isSuccess()){
                log.error(res.getMessage());
                return;
            }

            etcmDiseaseExServiceInf.getDownload(response,reqVo.getUiTypeCode(),reqVo.getLangCode(),reqVo.getExportType(),res.getData().getRecords());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @PostMapping("/herb/disease")
    @ApiOperation("herb_disease")
    public ResultModel<NetworkStatisticsRespVo> getHerbDisease(@RequestBody HerbHerbReqVo reqVo){
        return etcmDiseaseExServiceInf.getHerbDisease(reqVo);
    }

    @PostMapping("/herb/disease/page")
    @ApiOperation("herb_disease_table")
    public ResultModel<PageModel<Map<String,String>>> getHerbDiseasePage(@RequestBody HerbHerbPageReqVo reqVo){
        return etcmDiseaseExServiceInf.getHerbDiseasePage(reqVo);
    }

    @PostMapping("/herb/disease/download")
    @ApiOperation("herb_disease_download")
    public void getHerbDiseaseDownLoad(@ApiIgnore HttpServletResponse response,@RequestBody HerbHerbDownLoadReqVo reqVo){
        try {
            HerbHerbPageReqVo pageReqVo = new HerbHerbPageReqVo();
            BeanUtils.copyProperties(reqVo,pageReqVo);
            pageReqVo.setPageNo(1);
            pageReqVo.setPageSize(1000);
            ResultModel<PageModel<Map<String, String>>> res = etcmDiseaseExServiceInf.getHerbDiseasePage(pageReqVo);
            if (!res.isSuccess()){
                log.error(res.getMessage());
                return;
            }

            etcmDiseaseExServiceInf.getDownload(response,reqVo.getUiTypeCode(),reqVo.getLangCode(),reqVo.getExportType(),res.getData().getRecords());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @PostMapping("/ingr/disease")
    @ApiOperation("ingr_disease")
    public ResultModel<NetworkStatisticsRespVo> getIngrDisease(@RequestBody HerbHerbReqVo reqVo){
        return etcmDiseaseExServiceInf.getIngrDisease(reqVo);
    }

    @PostMapping("/ingr/disease/page")
    @ApiOperation("ingr_disease_table")
    public ResultModel<PageModel<Map<String,String>>> getIngrDiseasePage(@RequestBody HerbHerbPageReqVo reqVo){
        return etcmDiseaseExServiceInf.getIngrDiseasePage(reqVo);
    }

    @PostMapping("/ingr/disease/download")
    @ApiOperation("ingr_disease_download")
    public void getIngrDiseaseDownLoad(@ApiIgnore HttpServletResponse response, @RequestBody HerbHerbDownLoadReqVo reqVo){
        try {
            HerbHerbPageReqVo pageReqVo = new HerbHerbPageReqVo();
            BeanUtils.copyProperties(reqVo,pageReqVo);
            pageReqVo.setPageNo(1);
            pageReqVo.setPageSize(1000);
            ResultModel<PageModel<Map<String, String>>> res = etcmDiseaseExServiceInf.getIngrDiseasePage(pageReqVo);
            if (!res.isSuccess()){
                log.error(res.getMessage());
                return;
            }

            etcmDiseaseExServiceInf.getDownload(response,reqVo.getUiTypeCode(),reqVo.getLangCode(),reqVo.getExportType(),res.getData().getRecords());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    @GetMapping("/search/herb")
    @ApiOperation("搜索框 中草药")
    public ResultModel<List<SearchRespVo>> getHerbSearch(@RequestParam(required = false) String name, @RequestParam String langCode){
        return etcmDiseaseExServiceInf.getHerbSearch(name,langCode,CommonConsts.HERB);
    }

    @GetMapping("/search/px")
    @ApiOperation("搜索框 处方")
    public ResultModel<List<SearchRespVo>> getPxSearch(@RequestParam(required = false) String name, @RequestParam String langCode){
        return etcmDiseaseExServiceInf.getHerbSearch(name,langCode,CommonConsts.PX);
    }

    @GetMapping("/search/ingr")
    @ApiOperation("搜索框 成分")
    public ResultModel<List<SearchRespVo>> getIngrSearch(@RequestParam(required = false) String name, @RequestParam String langCode){
        return etcmDiseaseExServiceInf.getHerbSearch(name,langCode,CommonConsts.INGREDIENT);
    }

    @GetMapping("/search/disease")
    @ApiOperation("搜索框 中草药")
    public ResultModel<List<SearchRespVo>> getDiseaseSearch(@RequestParam(required = false) String name, @RequestParam String langCode){
        return etcmDiseaseExServiceInf.getHerbSearch(name,langCode,CommonConsts.DISEASE);
    }

}
